package com.crm.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.crm.entity.Executives;
import com.crm.util.ApplicationContext;
public class ExecutivesDAO {
	protected final Log logger = LogFactory.getLog(getClass());
	
	public  int insert(Executives e) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
			session.saveOrUpdate(e);
			tx.commit();
			return 0;
		}
		catch (Exception ex) {
		     if (tx!=null) tx.rollback();
		     throw ex;
		}
		finally {
		     session.close();
		}
	}
	public void deleteExecutive(int businessId, int execId) throws HibernateException, Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx=session.beginTransaction();
			String qtr="update Executives set is_deleted=true where id=:exid and business_id=:busid";
			Query qry=session.createQuery(qtr);
			qry.setInteger("busid", businessId);	
			qry.setInteger("exid", execId);	
			qry.executeUpdate();
			tx.commit();
		}
		catch (Exception ex) {
			 if (tx!=null) tx.rollback();
		     throw ex;
		}
		finally {
		     session.close();
		}
	}
	public List<Executives> findAll(int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		System.out.println("Session is conected "+ session.isConnected()+" is open "+session.isOpen());
		try {
			Query query=session.createQuery("from Executives where business_id=:busid and is_deleted=false");
			query.setInteger("busid", busid);
			List<Executives> liex=(List<Executives>)query.list();
			return liex;
		}
		catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public Executives findByName(String execName,int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			Query query=session.createQuery("from Executives where executive_name=:exName and  business_id=:busid");
			query.setInteger("busid", busid);
			query.setString("exName", execName);
			Executives liex=(Executives)query.uniqueResult();
			return liex;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
		     session.close();
		}
	}

	public List<Executives> getExec(int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			String qtr="From Executives where business_id=:busid and is_deleted=false";
			Query qry=session.createQuery(qtr);
			qry.setInteger("busid", busid);	
			List<Executives> qr=qry.list();	
			return qr;
		}
		catch (Exception ex) {
		     throw ex;
		}
		finally {
		     session.close();
		}
	}
	public Executives findById(int exid, int busid) throws HibernateException, Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			String qtr="From Executives where id=:exid and business_id=:busid";
			Query qry=session.createQuery(qtr);
			qry.setInteger("busid", busid);	
			qry.setInteger("exid", exid);	
			Executives qr=(Executives) qry.uniqueResult();
			return qr;
		}
		catch (Exception ex) {
		     throw ex;
		}
		finally {
		     session.close();
		}
	}
}
